package com.yesun.dcm.config.spring;

import com.yesun.dcm.config.spring.handler.DcmNamespaceHandler;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * @author chunmingdeng, 2017/10/20 Create.
 * @version 1.0
 * @copyright (c) YESUN. All Rights Reserved.
 * @description:
 */
public class RefererConfigBean<T> extends RefererConfig<T> implements FactoryBean<T>, BeanFactoryAware, ApplicationContextAware, InitializingBean {

    private transient BeanFactory beanFactory;
    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        //bean加载后，配置注册中心
        configRegistry();
    }

    private void configRegistry() {
        for (String name : DcmNamespaceHandler.registryDefineNames) {
            RegistryConfig registoryConfig = beanFactory.getBean(name, RegistryConfig.class);
            setRegistryConfig(registoryConfig);
        }
    }

    @Override
    public T getObject() throws Exception {
        return getRef();
    }

    @Override
    public Class<?> getObjectType() {
        return getInterface();
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

    }
}
